var Scene3dtitle = function(options) {
	this.ctx = options.ctx;
	this.editor = options.editor;
	this.sceneId = options.sceneId;
	this.globe = options.editor.globe;
	this.isOpen = false;
	this.dIndex;
	return this;
}

/**
 * 库
 */
Scene3dtitle.prototype.openLibrary = function(id) {
	let _this = this;
	if (_this.isOpen) {
		return;
	}
	layer.closeAll();
	layer.load(2);
	var content = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/libraryFor3dtitle", { sceneId: _this.sceneId }, "html", null);
	layer.closeAll('loading');
	if (content) {
		layer.open({
			id: '3dtitleLibrary',
			type: 1,
			anim: 5,
			title: '3dtitle模型库',
			area: '320px',
			content: content,
			shade: 0,
			offset: 'l',
			fixed: true,
			move: false,
			skin: 'layui-layer  layui-layer-adminRight layui-layer-city',
			success: function(layero, dIndex) {
				_this.dIndex = dIndex;
				_this.isOpen=true;
				$(layero).children('.layui-layer-content').css('overflow', 'hidden');
				$(layero).children('.layui-layer-content').css('height', 'calc(100% - 30px)');
				
				$("#keywords-3dtitle-input").on('input', function() {
					_this.load3dtitleModelList();
				})
				
				form.on('select(classifyId)', function(data){
					_this.load3dtitleModelList();
				})
				
				$("#keywords-3dtitle-input-name").on('input', function() {
					_this.load3dtitleModelList();
				})
				
				_this.load3dtitleModelList();
				form.render();
			},
			end: function() { 
				_this.isOpen=false;
			}
		});
	}else{
		layer.msg(res?res.message:'异常！', {icon: 2});
	}
}

//底图
Scene3dtitle.prototype.load3dtitleModelList = function() {
	var _this = this;
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/listDataForTileset", {
		sceneId:_this.sceneId,
		classifyId:$("#keywords-3dtitle-input").val(),
		name:$("#keywords-3dtitle-input-name").val()
	}, "json", null);
	if (0 == res.code) {
		res.ctx = _this.ctx;
		laytpl(scene3dtitleModelScript.innerHTML).render(res, function(html) {
			$('#select-scene-3dtitle-list').html(html);
			$(".OnlineMapList>.content>.item>.mapImge>.Thumbnail,.OnlineMapList>.content>.item>.mapImge>i").on('click', function() {
				  let  id=$(this).attr('id');
				  if( $("#item-"+id).hasClass("active")){
					  $("#item-"+id).removeClass("active")
					  _this.remove(id);
				  }else{
					  $("#item-"+id).addClass("active");
					  _this.add(id);
				  }
			})
		});
	}
}
/**
 * 添加
 */
Scene3dtitle.prototype.add=function(id){
	let _this=this;
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/getBySrcId", {
		thingId:id,
	}, "json", null);
    if (res && 200==res.code) {
    	let data=res.data;
    	let options={
    		id:	_this.globe.getUuid(),
    		name:data.name,
    		code:data.code||"",
    		type:'1',
			thing:{
				show:true,
				url:(data.isRelative=='0') ?data.url:_this.ctx+data.url,
			}
    	}
    	let entity=_this.globe.render(options);
    	if(entity){
    		entity.on('success',function(e){
                var formData={}; 
                formData['sceneModel.id']=entity.id;
                formData['sceneModel.name']=entity.name;
                formData['sceneModel.code']=entity.code;
                formData['sceneModel.type']=entity.type;
                formData['sceneModel.x']=entity.position.x;
                formData['sceneModel.y']=entity.position.y;
                formData['sceneModel.z']=entity.position.z;
                formData['sceneModel.heading']=entity.orientation.heading;
                formData['sceneModel.pitch']=entity.orientation.pitch;
                formData['sceneModel.roll']=entity.orientation.roll;
                formData['sceneModel.thingId']=id;
                formData['sceneModel.sceneId']=_this.sceneId;
    			var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/save",formData, "json", null);
    			layer.closeAll('loading');
    			if(200==res.code){
    				layer.msg(res?res.message:'系统异常!', {icon: 1});
    			}else{
    				 layer.msg(res?res.message:'系统异常!', {icon: 2});
    				_this.globe.remove(options.id);
    			}
    		});
    		entity.on('error',function(error){
    			layer.closeAll('loading');
    			layer.msg("模型加载异常！", {icon: 2});
    	    	_this.globe.remove(options.id);
    		});
    	}
    } else {
    	layer.closeAll('loading');
		if( $("#item-"+id).hasClass("active")) {
			$("#item-"+id).removeClass("active")
			_this.remove(id);
		}
        layer.msg(res?res.message:'系统异常!', {icon: 2});
    }
}

Scene3dtitle.prototype.editTModel=function(obj){
	var _this = this;
	
	if(_this.model && _this.model.id!=obj.id){
		_this.closeEditTModelTool();
	}
	_this.model=obj;
	if (_this.isOpen) {
		_this.loadForm(obj);
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/edit3dtitle", {}, "html", null);
	layer.closeAll('loading');
	if (!res) {
		layer.alert('系统异常！');
		return;
	}
	
	layer.open({
		id: 'modelLibrary',
		title: '属性编辑',
		type: 1,
		offset: '40px',
		area: '300px',
		content: res,
		shade: 0,
		offset: 'l',
		skin: 'layui-layer layui-layer-adminRight layui-layer-city',
		success: function(layero, dIndex) {
			_this.isOpen = true;
			_this.pdIndex = dIndex;
			$(layero).children('.layui-layer-content').css('overflow', 'hidden');
			$(layero).children('.layui-layer-content').css('height', 'calc(100% - 30px)');
			form.render();
			_this.loadForm(obj);
			
			$("#pmodelTranslationBtn").on('click', function() {
				if(!$(this).hasClass("scene-btn-disabled")){
					$(this).addClass("scene-btn-disabled");
					_this.createEditTModelTool(_this.model);
				}else{
					$(this).removeClass("scene-btn-disabled");
					_this.closeEditTModelTool();
				}
			})
		},
		end: function() {
			_this.isOpen = false;
			_this.dIndex = null;
			_this.model=null;
			_this.closeEditTModelTool();
			//_this.closeEditTool();
		}
	});
}

Scene3dtitle.prototype.loadForm = function(model){
	var _this = this;
	var data=this.getFormData(model);
	form.val('modelEditForm', data);
	
	$('#sceneModelName').on('input', function() {
		 if(model){
			 model.updateAttribute({
				 name:$("#sceneModelName").val(),
			 });
		 }
	 });
	$('#sceneModelCode').on('input', function() {
		 if(model){
			 model.updateAttribute({
				 code:$("#sceneModelCode").val()
			 });
		 }
	 });
	$('#sceneModelSort').on('input', function() {
		 if(model){
			 model.updateAttribute({
				 sort:$("#sceneModelSort").val()
			 });
		 }
	 });
	
	$('#sceneModelScale').on('input', function() {
		 if(model){
			 model.updateScale($("#sceneModelScale").val());
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });
	
	$('#sceneModelHeading').on('input', function() {
		 if(model){
			 model.updateHeading($("#sceneModelHeading").val());
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });
	
	$('#sceneModelPitch').on('input', function() {
		 if(model){
			 model.updatePitch($("#sceneModelPitch").val());
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });	 
	 
	$('#sceneModelRoll').on('input', function() {
		 if(model){
			 model.updateRoll($("#sceneModelRoll").val());
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });	
	
	$('#modelZ').on('input', function() {
		 if(model){
			 model.updatePosition({
				 x:$("#modelX").val(),
				 y:$("#modelY").val(),
				 z:$("#modelZ").val(),
			 });
		 }
	 });
	
	$('#sceneModelPositionBtn').click(function() {
		_this.globe.flyToPoisition(data);
	});
	
	$('#sceneModelDeleteBtn').click(function() {
		layer.confirm('确定要删除模型吗？', {
			shade: .1
		}, function(index) {
			_this.delete(data);
		}, function(index) {
			layer.close(index);
			return false;
		});
	});
	
	
	
	form.on('submit(submitModelEditForm)', function(data) {
		layer.load(2);
		var formData = {};
		for (var key in data.field) {
			formData['sceneModel.' + key] = data.field[key];
		}
		var res = common_ajax.ajaxFunc('post', _this.ctx + "/scene/model/save", formData, "json", null);
		layer.closeAll('loading');
		if (res && 200 == res.code) {
			layer.msg(res.message, {icon: 1});
			_this.updateThing(data.field);
			_this.editor.sceneLayer.refreshNode();
		} else {
			layer.msg(res.message, {icon: 2});
		}
		return false;
	});
}

Scene3dtitle.prototype.getFormData=function(obj){
	let data={};
	if(obj){
		data={
			id:obj.id,
			name:obj.name||'',
			code:obj.code||'',
			
			x:obj.position.x||'',
			y:obj.position.y||'',
			z:obj.position.z||0,
			
			heading:obj.orientation.heading||0,
			pitch:obj.orientation.pitch||0,
			roll:obj.orientation.roll||0,
			
			scale:obj.model.scale||1,
			preferLeaves:obj.model.preferLeaves||false,
			skipLevels:obj.model.skipLevels||1,
			cacheFeature:obj.model.cacheFeature||0,
			baseScreenSpaceError:obj.model.baseScreenSpaceError||1024,
			skipScreenSpaceErrorFactor:obj.model.skipScreenSpaceErrorFactor||16,
			maximumMemoryUsage:obj.model.maximumMemoryUsage||512,
			maximumScreenSpaceError:obj.model.maximumScreenSpaceError||16,
			skipLevelOfDetail:obj.model.skipLevelOfDetail||false,
			maximumNumberOfLoadedTiles:obj.model.maximumNumberOfLoadedTiles||1024,
		}	
	}
	return data;
}

Scene3dtitle.prototype.updateThing=function(data){
	this.model.updateThing(data);
}

Scene3dtitle.prototype.remove=function(id){
	let _this=this;
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/delete", {
		thingId:id,
		sceneId:_this.sceneId,
	}, "json", null);
	layer.closeAll('loading');
    if (res && 200==res.code) {
    	_this.globe.remove(res.data.id);
    } else {
        layer.msg(res?res.message:'系统异常!', {icon: 2});
    }
}

Scene3dtitle.prototype.createEditTModelTool=function(model){
	let _this=this;
	_this.editTModelTool=_this.globe.createEditTModel(model);
	_this.editTModelTool.on('change',function(e){
		if(e.position){
			$("#modelX").val(e.position.x)
			$("#modelY").val(e.position.y)
			$("#modelZ").val(e.position.z)
		}
	});
}

Scene3dtitle.prototype.closeEditTModelTool=function(){
	let _this=this;
	if(_this.editTModelTool){
		_this.editTModelTool.destroy();
		_this.editTModelTool=null;
	}
}

